#include "getsystemrunningstateres.h"

GetSystemRunningStateRes::GetSystemRunningStateRes(): BaseMsg(MSG_TYPE_GET_SYSTEM_RUNNING_STATE_RES)
{

}

int GetSystemRunningStateRes::decodeParametersFromTlv(TLV *tlv)
{

    if(tlv->tag == GetSystemRunningStateRes_tag_disk)
    {
        DiskInfoTlvEncodedObj obj;
        obj.decodeParametersFromBuffer((byte *)tlv->buffer, tlv->length);
        lastDiskInfo.push_back(obj);
    }
    else if (tlv->tag == GetSystemRunningStateRes_tag_memory)
    {
        SString mems = tlv->buffer;
        QStringList memList = QString::fromStdString(mems).split(",");
        foreach (QString mem, memList) {
            lastMEMUsages.push_back(mem.toFloat());
        }
    }
    else if (tlv->tag == GetSystemRunningStateRes_tag_cpu)
    {
        SString cpus = tlv->buffer;
        QStringList cpuList = QString::fromStdString(cpus).split(",");
        foreach (QString cpu, cpuList) {
            lastCPUUsages.push_back(cpu.toFloat());
        }

    }else
    {
        LOG_ERROR("unexpedted tlv tag:%d", tlv->tag);
    }

    return f_success;

}

int GetSystemRunningStateRes::encodeParametersIntoTlvs(list<TLV *> &tlvs)
{

    QString memorys = "";
    int memIndex = 0;
    foreach (float mem, lastMEMUsages) {
        QString memory = float2QStr(mem);
        memorys.append(memory);
        if(memIndex < lastMEMUsages.size()-1){
            memorys.append(",");
        }
        memIndex++;
    }
    tlvs.push_back(createTlvByStringValue(GetSystemRunningStateRes_tag_memory, memorys.toStdString()));


    foreach (DiskInfoTlvEncodedObj obj, lastDiskInfo) {
        tlvs.push_back(createTlvByTLVEncodedObject(GetSystemRunningStateRes_tag_disk, &obj));
    }


    QString cpus = "";
    int cpuIndex = 0;
    foreach (float cpu, lastCPUUsages) {
        QString cpuStr = float2QStr(cpu);
        cpus.append(cpuStr);
        if(cpuIndex < lastCPUUsages.size()-1){
            cpus.append(",");
        }
        cpuIndex++;
    }
    tlvs.push_back(createTlvByStringValue(GetSystemRunningStateRes_tag_cpu, cpus.toStdString()));

    return f_success;
}
